<template>
    <div style="width: 100%; height: 100vh; background-color: darkblue; overflow:hidden">    <!--background-color: darkblue;背景色 overflow隐藏上层的空白-->
        <div style="width: 400px; margin: 150px auto">
            <div style="color: chartreuse; font-size: 30px; text-align: center; padding: 30px 0 30px 20px;">欢迎注册</div>
            <el-form ref="form" :model="form" size="normal" :rules="rules">
                <!--图标问题-->
                <el-form-item prop="username">
                    <el-input v-model="form.username" :prefix-icon="Avatar"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="form.password" show-password :prefix-icon="Lock"></el-input>
                </el-form-item>
                <el-form-item prop="confirm">
                    <el-input v-model="form.confirm" show-password :prefix-icon="Lock"></el-input>
                </el-form-item>
                <el-form-item label-width="138px">
                    <el-button type="primary" style="width: 60%" @click="register">注册</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import request from "../utils/request";
    import { Avatar, Lock } from "@element-plus/icons";
    export default {
        name: "Register",
        data(){
            return {
                form: {},
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ],
                    confirm: [
                        {required: true, message: '请再次输入密码', trigger: 'blur'},
                    ]
                }
            }
        },
        methods: {
            register(){
                if (this.form.password !== this.form.confirm){
                    this.$message({
                        type: "error",
                        message: "两次输入密码不一致!"
                    });
                    return ;
                }
                this.$refs['form'].validate((valid) => {
                    if (valid){
                        request.post("/user/register",this.form).then(res => {
                            console.log(res);
                            if (res.code === '0'){
                                this.$message({
                                    type: "success",
                                    message: "注册成功",
                                });
                                this.$router.push("/login"); //登录成功进行跳转
                            }else {
                                this.$message({
                                    type: "error",
                                    message: res.msg,
                                })
                            }
                        })
                    }
                });
            }
        },
        setup(){
            return {
                Avatar,
                Lock
            }
        },
    }
</script>

<style scoped>

</style>